from flask import Flask, url_for, request
from markupsafe import escape

app = Flask(__name__)


@app.route("/")
def hello_world():
    return later()


@app.route("/<name>")
def hello(name):
    return f'{later_index_url()}Hello, {name}! Are you ZSJ?'


@app.route("/es/<name>")
def escaped_hello(name):
    return f'{later_index_url()}Hello, {escape(name)}!'


@app.route("/name/<name>/num/<int:num>")
def hello_num(name, num):
    s = later_index_url()
    for i in range(num):
        s += f'{i:03d}: Hello, {name}! Are you ZSJ?<br />'
    return s


with app.test_request_context():
    print(request.method, request.path)
    print(url_for("hello_world"))
    print(url_for('hello', name='Zhang S. Jing'))
    print(url_for('hello_num', name='Mss. Ting Yang', num=8))

with app.test_request_context('/name/PYP/num/7', method='POST'):
    print(request.method, request.path)
    print(url_for("hello_world"))
    print(url_for('hello', name='Zhang S. Jing'))
    print(url_for('hello_num', name='Mss. Ting Yang', num=8))


def later():
    return f"""\
<p>Hello, World!</p>
{later_index_url()}
<a href="{url_for('hello', name='Zhang S. Jing')}">hello zsj</a>
<a href="{url_for('hello_num', name='Mss. Ting Yang', num=8)}">hello num</a>
"""


def later_index_url():
    return f'<link rel="stylesheet" type="text/css" href="/static/main.css" /><a href="{url_for("hello_world")}">hello world</a>'